package com.yuandengta.yomall.search.controller;

import com.yuandengta.common.exception.BizCode;
import com.yuandengta.common.to.es.SkuEsModel;
import com.yuandengta.common.utils.R;
import com.yuandengta.yomall.search.service.ProductServcie;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * ElasticSearch 存储 {@link ElasticSaveController}
 *
 * @author Kevin
 * @email: 178676392@qq.com
 */
@Slf4j
@RequestMapping("/search/save")
@RestController
public class ElasticSaveController {

    @Autowired
    private ProductServcie productServcie;

    /**
     * 商品上架
     * @param skuEsModelList
     * @return
     */
    @PostMapping("/product")
    public R productUp(@RequestBody List<SkuEsModel> skuEsModelList) {
        boolean flag = false;
        try {
            // 返回 false，说明商品上架没有异常
            flag = productServcie.productUp(skuEsModelList);
        } catch (IOException e) {
            log.error("ElasticSaveController商品上架错误：{}", e);
            return R.error(BizCode.PRODUCT_UP_EXCEPTION.getCode(), BizCode.PRODUCT_UP_EXCEPTION.getMessage());
        }

        if (!flag) {
            return R.ok();
        } else {
            return R.error(BizCode.PRODUCT_UP_EXCEPTION.getCode(), BizCode.PRODUCT_UP_EXCEPTION.getMessage());
        }
    }
}
